﻿#include "pch.h"
#include "HexEditor.h"
#include "afxdialogex.h"
#include "CEditPage.h"
#include "MainFrm.h"
extern CStatusBar g_wndStatusBar;

IMPLEMENT_DYNAMIC(CEditPage, CPropertyPage)

CEditPage::CEditPage()
	: CPropertyPage(IDD_DIALOG_PAGE) {
}

CEditPage::~CEditPage() {
	if (m_pMenuWnd) {
		delete m_pMenuWnd;
		m_pMenuWnd = nullptr;
	}
}

BOOL CEditPage::IsDynamicChecked() {
	CMainFrame* pMainFrame = (CMainFrame*)AfxGetMainWnd();
	if (pMainFrame) {
		return pMainFrame->IsDynamicChecked();
	}
	return FALSE;
}

BOOL CEditPage::IsStaticChecked() {
	CMainFrame* pMainFrame = (CMainFrame*)AfxGetMainWnd();
	if (pMainFrame) {
		return pMainFrame->IsStaticChecked();
	}
	return FALSE;
}

void CEditPage::DoDataExchange(CDataExchange* pDX) {
	CPropertyPage::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT_HEX, m_HexEdit);
}

BOOL CEditPage::OnInitDialog() {
	CPropertyPage::OnInitDialog();
	return TRUE; // 成功
}

BOOL CEditPage::OnSetActive() {
	if (m_pMenuWnd) {
		delete m_pMenuWnd;
		m_pMenuWnd = nullptr;
	}
	CMainFrame* pMainFrame = (CMainFrame*)AfxGetMainWnd();
	g_wndStatusBar.SetPaneText(1, L"光标位置数据大小");
	g_wndStatusBar.SetPaneText(2, L"光标位置");
	g_wndStatusBar.SetPaneText(3, L"起始结束位置及大小");
	if (pMainFrame) {
		pMainFrame->UpdateStatusBar();
	}
	return CPropertyPage::OnSetActive();
}

BOOL CEditPage::OnKillActive() {
	if (m_pMenuWnd) {
		delete m_pMenuWnd;
		m_pMenuWnd = nullptr;
	}
	return CPropertyPage::OnKillActive();
}

void CEditPage::InitMenuItems() {
	m_menuItems = {
		{ _T("关闭"), ID_CLOSE },
		{ _T("另存为"), ID_SAVE_TO },
		{ _T("减少一列"), ID_SUB },
		{ _T("增加一列"), ID_ADD },
		{ _T("转到"), ID_JUMP },
		{ _T("填充"), ID_FILL },
		{ _T("查找"), ID_FIND },
		{ _T("下一个"), ID_FIND_NEXT },
		{ _T("刷新"), ID_RELOAD },
		{ _T("保存"), ID_FILE_SAVE }
	};
}

void CEditPage::OnContextMenu(CWnd* pWnd, CPoint point) {
	// 初始化菜单项
	InitMenuItems();
	// 创建自定义菜单窗口
	if (m_pMenuWnd) {
		delete m_pMenuWnd;
		m_pMenuWnd = nullptr;
	}

	if (!m_pMenuWnd) {
		m_pMenuWnd = new CHorizontalMenuWnd();
		m_pMenuWnd->Create(this);
		m_pMenuWnd->SetMenuItems(m_menuItems);
	}
	CPoint clientPoint(0, 0);
	ClientToScreen(&clientPoint);

	if (m_pMenuWnd) {
		m_pMenuWnd->SetMenuItems(m_menuItems); // 确保设置当前页面的菜单项
		m_pMenuWnd->ShowMenu(clientPoint);
		m_bInitialized = TRUE;
	}
}

BEGIN_MESSAGE_MAP(CEditPage, CPropertyPage)
	ON_WM_CONTEXTMENU()
END_MESSAGE_MAP()